<?php

Class Chance{
    
    /**
     * array('id'=>'%', 'id'=>'%')
     * %*count(array) <= 100 
     * return 'id'
    */
    function get($arr){
        $chance = array();
        #return array_sum($arr);
        if(array_sum($arr) > 100){
            $co = 100/array_sum($arr);
        }else{
            $co = 1;
        }
        #return $co;
        
        foreach($arr as $key => $val){
            $val = $val * $co;
            #$test[] = $val;
            for($i = 1; $i <= floatval($val); $i++){
                $chance[] = $key;         
            }
        }
        #return $test;
        #return count($chance);
        if(count($chance) > 100) return false;
        
        if(count($chance) < 100){
            for($i = count($chance); $i <= 99; $i++){
                $chance[] = 0;
            }
        }
        #return $chance;
        #return rand(1, count($chance));        
        #return count($chance);
        return( $chance[rand(1, count($chance))] );
    }
    
    /**
     * array('id'=>'', 'id'=>'') 
     * return array('id'=>'%', 'id'=>'%')
    */
    function getToPercent($arr){
        $summ = 0;
        foreach($arr as $key => $val){
            $summ = $summ + floatval($val);    
        }
        $p = $summ/100;
        foreach($arr as $key => $val){
            $arr[$key] =  (floatval($val) > 0 )? intval(floatval($val)/$p) : 0 ;    
            #$arr[$key] =  intval( max(0.001, floatval($val) )/$p ) ;
        }
        return $arr;
    }
}
?>